package com.example.xm.controller;

import com.example.xm.entity.Productentity;
import com.example.xm.entity.Userentity;
import com.example.xm.entity.dto.ProductDto;
import com.example.xm.entity.dto.ProductQueryDto;
import com.example.xm.entity.dto.UserDto;
import com.example.xm.entity.vo.EcahrsVo;
import com.example.xm.entity.vo.ProductListVo;
import com.example.xm.service.ProductService;
import com.example.xm.web.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    @PostMapping("/reg")
    public Result reg(@RequestBody ProductDto dto){
    productService.add(dto);
    return Result.success();
}
    @PostMapping("/update")
    public Result update(@RequestBody ProductDto dto){
        productService.update(dto);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(ProductQueryDto dto){
        PageInfo<Productentity> pageInfo= productService.list(dto);
        return Result.success(pageInfo);
    }  @GetMapping("/xiaoliang")
    public Result xiaoliang(){
        List<Productentity> pageInfo= productService.xiaoliang();
        return Result.success(pageInfo);
    }
@GetMapping("/{id}")
    private Result getById(@PathVariable Integer id){
  ProductListVo productListVo= productService.getById(id);
  return Result.success(productListVo);
}
    @GetMapping("/echarts")
    private Result echarts(Integer num,Integer type){
        List<EcahrsVo> echarts = productService.echarts(num,type);
        return Result.success(echarts);
    }
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids){
        String[] split = ids.split(",");
        int[] intArray = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            intArray[i] = Integer.parseInt(split[i]);
        }
        productService.delete(intArray);
        return Result.success();
    }
}
